package cn.hospital.controller;

import cn.hospital.entity.Department;
import cn.hospital.entity.Grouptable;
import cn.hospital.entity.Hospital;
import cn.hospital.entity.MeaningUser;
import cn.hospital.service.DropDownBoxQueryService;
import cn.hospital.service.GroupingService;
import cn.hospital.util.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/group")
public class GroupingController {

    @Resource
    private GroupingService groupingService;

    //新建随访组---------------------------------------------------------------------------------------------------------
    /**
     * 新建随访组
     * @return
     */
    @RequestMapping("/select")
    public String groupingSelect(Model model, HttpSession session){
        MeaningUser meaningUser=(MeaningUser) session.getAttribute("meaningUser");
        Department department = groupingService.depeartmentId(meaningUser.getDepartmentId());
        model.addAttribute("department",department);
        model.addAttribute("meaningUser",meaningUser);
        return  "page/grouping/addgrouping";
    }

    /**
     * 添加随访组信息
     * @param grouptable
     * @return
     */
    @RequestMapping("/insertGrouptable")
    public String groupingInsert(Grouptable grouptable) throws ParseException {
        if(grouptable!=null) {
            groupingService.groupingAdd(grouptable);
        }
        return "redirect:/group/list";
    }

    //随访组---------------------------------------------------------------------------------------------------------

    /**
     * 删除随访组信息
     * @param groupId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public int DeleteGrouptable(Integer[] groupId){
        int shan=0;
        if (groupId != null) {
           shan=groupingService.groupDelete(groupId);
        }
        return shan;
    }

    /**
     * 更新分组信息
     * @param grouptable
     * @return
     * @throws ParseException
     */
    @RequestMapping("/update")
    @ResponseBody
    public int groupUpdate(Grouptable grouptable) throws ParseException {
        int updateNumber=groupingService.groupUpdate(grouptable);
        return updateNumber;
    }

    /**
     * 查询某个分组信息
     * @return
     */
    @RequestMapping("/selectOne")
    @ResponseBody
    public Grouptable selectGroupOne(Integer groupId){
        return  groupingService.selectGroupOne(groupId);
    }

    /**
     * 检索分组信息分页展示
     * @param grouptable
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String groupingList(Grouptable grouptable, Model model, Integer currentPage) throws ParseException {
        PageInfo<Grouptable> grouptableList=null;
        grouptableList=groupingService.groupingList(grouptable,currentPage);
        List<Department> departmentList=groupingService.depeartmentfin("1");
        model.addAttribute("grouptableList",grouptableList);
        model.addAttribute("grouptable",grouptable);
        model.addAttribute("departmentList",departmentList);
        return "page/grouping/grouping";
    }

    /**
     * 查询随访计划
     *
     * @param map
     * @return
     */
    @RequestMapping("/groupingAndDepartment")
    public String groupingAndDepartment(ModelMap map) {
        List<Grouptable> grouptables = groupingService.findGrouptablr();
        map.addAttribute("grouptables", grouptables);
        return "page/grouping/follow_up_plan";
    }
}
